package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.widget.FlexBoxLayoutManager;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.statistics.StatisticsItemInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.StatisticsItemAdapter;

import java.util.List;

/**
 * @author fgs
 * @date 2021/2/23
 * @description
 **/
public class StatisticsInfoView extends LinearLayout {
    private TextView titleTextView;
    private RecyclerView contentRecyclerView;
    private StatisticsItemAdapter itemAdapter;

    public StatisticsInfoView(Context context) {
        this(context, null);
    }

    public StatisticsInfoView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatisticsInfoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StatisticsInfoView);
        String title;
        try {
            title = typedArray.getString(R.styleable.StatisticsInfoView_StatisticsInfoViewTitle);
        } finally {
            typedArray.recycle();
        }

        View view = LayoutInflater.from(context).inflate(R.layout.layout_statistics_info_view, null);
        titleTextView = view.findViewById(R.id.layout_statistics_info_view_titleTextView);
        contentRecyclerView = view.findViewById(R.id.layout_statistics_info_view_contentRecyclerView);

        itemAdapter = new StatisticsItemAdapter(context);
        contentRecyclerView.setLayoutManager(new FlexBoxLayoutManager(context));
        contentRecyclerView.setAdapter(itemAdapter);

        addView(view);

        setInfoTitle(title);

    }

    public void setInfoTitle(String title) {
        titleTextView.setText(title);

    }

    public void setStatisticsInfoData(List<StatisticsItemInfo> infoList) {
        itemAdapter.addAll(infoList);
    }
}
